<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Dictionary;

class DictionaryController extends Controller
{
	/**
	 * 查询数据字典
	 * @return [type] [description]
	 */
   	public function show( Request $request )
   	{
		$num  = $request->input('num')?$request->input('num'):20;
		
		$info = Dictionary::where('id', '<>', 1)->orderBy('id', 'desc')->paginate($num);

   		return self::json($info);
   	}

   	/**
   	 * 添加数据字典
   	 * @param  Requset $request [description]
   	 * @return [type]           [description]
   	 */
   	public function create( Request $request )
   	{
		$title    = $request->input('title');
		$name     = $request->input('name');
		$contents = $request->input('contents');
		$url      = $request->input('url');
		$note     = $request->input('note');
		
		Dictionary::create([
				'title'    => $title,
				'name'     => $name,
				'contents' => $contents,
				'url'      => $url,
				'note'     => $note,
			]);

		Dictionary::where('id', 1)->increment('contents');

		return self::success('创建成功');
   	}

   	/**
   	 * 修改数据字典
   	 * @param  Request $request [description]
   	 * @return [type]           [description]
   	 */
   	public function edit( Request $request )
   	{
		$title    = $request->input('title');
		$name     = $request->input('name');
		$contents = $request->input('contents');
		$url      = $request->input('url');
		$note     = $request->input('note');
		$status   = $request->input('status');
		$id       = $request->input('id');

		$info = Dictionary::find($id);

		if ( !$info) {
			return self::error('请传递正确的数据');
		}

		$info->title    = $title;
		$info->name     = $name;
		$info->contents = $contents;
		$info->url      = $url;
		$info->note     = $note;
		$info->status   = $status;

		$info->save();

		Dictionary::where('id', 1)->increment('contents');

		return self::success('修改成功');
   	}

   	/**
   	 * 删除
   	 * @param  Request $request [description]
   	 * @return [type]           [description]
   	 */
   	public function del( Request $request)
   	{
   		$id = $request->input('id');

   		Dictionary::destroy($id);

   		return self::success('删除成功');
   	}
}
